﻿using HZU.ZP.Data.Interfaces;
using HZU.ZP.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HZU.ZP.Data.Impl
{
    public class CommentDao : ICommentDao
    {
        private EFCoreDbContext _context;

        public CommentDao(EFCoreDbContext context)
        {
            _context = context;
        }
        public void Add(Comment comment)
        {
            _context.Comments.Add(comment);
            _context.SaveChanges();
        }

        public void Delete(int id)
        {
            var comment = _context.Comments.FirstOrDefault(e => e.Id == id);
            _context.Comments.Remove(comment);
            _context.SaveChanges();
        }

        public IList<Comment> GetByCommentId(int commentId)
        {
            return _context.Comments.Where(e => e.CommentId == commentId).ToList();
        }

        public int GetByNoticeCount(int noticeId)
        {
            return _context.Comments.Where(e => e.NoticeId == noticeId && e.CommentId == 0).Count();
        }

        public IList<Comment> GetByNoticeId(int noticeId, int pageIndex)
        {
            return _context.Comments.Where(e => e.NoticeId == noticeId && e.CommentId == 0).Skip((pageIndex-1)*10).Take(10).ToList();
        }
    }
}
